package com.heima.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.feign.ArticleFeignClient;
import com.heima.mapper.ChannelMapper;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.service.ChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author chenshan
 */
@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, WmChannel> implements ChannelService {
    @Autowired
    private ArticleFeignClient articleFeignClient;

    /**
     * 查询所有频道信息
     *
     * @return ResponseResult
     */
    @Override
    public ResponseResult channelList() {
        return ResponseResult.okResult(list(Wrappers.lambdaQuery(WmChannel.class)
                .eq(WmChannel::getStatus, true).orderByAsc(WmChannel::getOrd)));
    }

    /**
     * 频道名称模糊分页查询
     *
     * @param channelDto 频道分页参数
     * @return ResponseResult
     */
    @Override
    public ResponseResult pageFindChannels(ChannelDto channelDto) {
        // 校验参数的合法性
        if (ObjectUtil.isEmpty(channelDto.getPage())) {
            channelDto.setPage(0);
        }

        if (ObjectUtil.isEmpty(channelDto.getSize())) {
            channelDto.setSize(10);
        }

        // 开启分页
        Page<WmChannel> page = new Page<>(channelDto.getPage(), channelDto.getSize());
        page(page, Wrappers.lambdaQuery(WmChannel.class).eq(ObjectUtil.isNotEmpty(channelDto.getName()), WmChannel::getName, channelDto.getName()));

        PageResponseResult result = new PageResponseResult();
        result.setCurrentPage((int) page.getCurrent());
        result.setSize((int) page.getSize());
        result.setTotal((int) page.getTotal());
        result.setData(page.getRecords());
        result.setHost("");


        return result;
    }

    /**
     * 修改频道信息
     * @param channel 频道信息
     * @return ResponseResult
     */
    @Override
    public ResponseResult updateChannel(WmChannel channel) {
        // 校验参数的合法性
        if (ObjectUtil.isNull(channel.getId())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        updateById(channel);

        return ResponseResult.okResult(null);
    }

    /**
     * 新增频道信息
     * @param channel 频道信息
     * @return ResponseResult
     */
    @Override
    public ResponseResult saveChannel(WmChannel channel) {
        // 校验参数的合法性
        if (!ObjectUtil.isAllNotEmpty(channel.getName(), channel.getDescription(), channel.getStatus(), channel.getOrd())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        save(channel);

        return ResponseResult.okResult(null);
    }

    /**
     * 删除频道信息
     * @param id 频道id
     * @return ResponseResult
     */
    @Override
    public ResponseResult delChannel(Integer id) {
        // 校验参数的合法性
        if (ObjectUtil.isEmpty(id)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 判断当前频道是否有文章存在
        ResponseResult result = articleFeignClient.findArticleByChannelId(id);
        if (result.getCode() != 200) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH, "频道中有数据，不允许删除~");
        }

        removeById(id);

        return ResponseResult.okResult(null);
    }
}
